package com.atstudy.service;

import com.atstudy.dao.AccountDao;
import com.atstudy.entites.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;
import java.util.List;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2023/1/31 9:23
 * @copyright: 博为峰
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Override
    public List<Account> getAccountList() {
        return accountDao.selectAccountList();
    }

    @Override
    public int transfer(final String from, final String to, final int balance) {
        accountDao.updateAccount(from, -balance);
        System.out.println(1 / 0);
        accountDao.updateAccount(to, balance);
        return 0;
    }

    /*@Override
    public int transfer(final String from, final String to, final int balance) {
        transactionTemplate.execute(new TransactionCallback<Object>() {
            @Override
            public Object doInTransaction(TransactionStatus status) {
                accountDao.updateAccount(from, -balance);
                System.out.println(1 / 0);
                accountDao.updateAccount(to, balance);
                return null;
            }
        });
        return 0;
    }*/

    /*@Override
    public int transfer(String from, String to, int balance) {
        //1.事务管理器
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        //2.事务定义
        DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
        //3.事务隔离级别（数据库）
        transactionDefinition.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
        //4.事务传播行为（语言）
        transactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        //5.超时时间
        transactionDefinition.setTimeout(-1);
        //6.是否只读
        transactionDefinition.setReadOnly(false);

        //事务状态
        TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);

        try {
            accountDao.updateAccount(from, -balance);
            System.out.println(1 / 0);
            accountDao.updateAccount(to, balance);
            transactionManager.commit(transactionStatus);
        } catch (Exception e) {
//            e.printStackTrace();
            transactionManager.rollback(transactionStatus);
        }
        return 0;
    }*/
}
